package com.lilei.bigdata.hive.mrbean;


import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Set;


public class WebLogParser {

	public static SimpleDateFormat df1 = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US);
	public static SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);

	public static WebLogBean parser(String line) {
			WebLogBean webLogBean = new WebLogBean();
			String[] arrs =  line.split(" ");
			if (arrs.length >11){
				webLogBean.setRemote_addr(arrs[0]);
				webLogBean.setRemote_user(arrs[1]);
				String time_local = formatDate(arrs[3].substring(1));
				if (StringUtils.isEmpty(time_local)){
					time_local="-invilid_time-";
				}
				webLogBean.setTime_local(time_local);
				webLogBean.setRequest(arrs[6]);
				webLogBean.setStatus(arrs[8]);
				webLogBean.setBody_bytes_sent(arrs[9]);
				webLogBean.setHttp_referer(arrs[10]);
				//如果长度大于12 则说明useragent 被多个空格截取
				if (arrs.length >12){
					StringBuilder sb = new StringBuilder();
					for(int i =11;i<arrs.length;i++){
						sb.append(arrs[i]);
					}
					webLogBean.setHttp_user_agent(sb.toString());
				}else {
					webLogBean.setHttp_user_agent(arrs[11]);
				}

				//如果状态码大于400 也判断当前访问无效
				if (Integer.parseInt(webLogBean.getStatus()) >=400){
					webLogBean.setValid(false);
				}

				if ("-invilid_time-".equals(webLogBean.getTime_local())){
					webLogBean.setValid(false);
				}
			}else {
				webLogBean.setValid(false);
			}
			return webLogBean;
	}

	public static void filtStaticResource(WebLogBean bean, Set<String> pages) {
		if (!pages.contains(bean.getRequest())) {
			bean.setValid(false);
		}
	}

	public static String formatDate(String time_local) {
		try {
			return df2.format(df1.parse(time_local));
		} catch (ParseException e) {
			return null;
		}

	}

}
